import axios from "axios"
import Vue from "vue"

import { BASE_URL } from '../config'
import store from '../store'
import router from '../router'
Vue.mixin({
    //给vue混入通用的方法
    methods: {
        //get方法
        async $get(url, params) {
            if (!url.startsWith("http") && !url.endsWith(".json")) {
                url = BASE_URL + url;
            }
            if (store.state.login.isAuthenticated == true) {
                axios.defaults.headers.common['token'] = store.state.login.token
            }
            let { data } = await axios.get(url, { params });
            if (data.code == "403") {
                this.$alert('验证失效，请重新登陆')
                router.push({ path: '/' })
            } else {
                console.log('get请求得到data数据为')
                console.log(data)
                return data;
            }
        },
        async $post(url, params) {
            if (!url.startsWith("http") && !url.endsWith(".json")) {
                url = BASE_URL + url;
            }
            if (store.state.login.isAuthenticated == true) {
                axios.defaults.headers.common['token'] = store.state.login.token
            }
            axios.defaults.withCredentials = true
            let { data } = await axios.post(url, params)
            if (data.code == "403") {
                this.$alert('验证失效，请重新登陆')
                router.push({ path: '/' })
            } else {
                console.log('get请求得到data数据为')
                console.log(data)
                return data;
            }

        },
        async $deleteImg(url, params) {
            if (!url.startsWith("http") && !url.endsWith(".json")) {
                url = BASE_URL + url;
            }
            if (store.state.login.isAuthenticated == true) {
                axios.defaults.headers.common['token'] = store.state.login.token
            }
            let { data } = await axios.delete(url, { data: params })
            if (data.code == "403") {
                this.$alert('验证失效，请重新登陆')
                router.push({ path: '/' })
            } else {
                console.log('get请求得到data数据为')
                console.log(data)
                return data;
            }

        },
        $downloadImg(url, imgIds) {
            if (store.state.login.isAuthenticated == true) {
                axios.defaults.headers.common['token'] = store.state.login.token
            }
            let param = ""
            for (var element of imgIds) {
                param += "imageIds" + "=" + element + "&";
            }
            param = param.slice(0, param.length - 1);
            console.log(param);
            url = BASE_URL + url;
            axios.get(url + '?' + param, { responseType: 'blob' }).then(res => {
                if (res.code == "403") {
                    this.$alert('token已经失效')
                    router.push({ path: '/' })
                } else {
                    let blob = new Blob([res.data]);
                    console.log(blob)
                    let objectUrl = window.URL.createObjectURL(blob);
                    let aTag = document.createElement("a");
                    aTag.setAttribute("href", objectUrl);
                    aTag.setAttribute("style.display", "none")
                    if (imgIds.length == 1) {
                        aTag.setAttribute("download", "filename.png");
                    } else {
                        aTag.setAttribute("download", "filename.zip");
                    }

                    aTag.click();
                    // 释放创建的文件地址对象
                    window.URL.revokeObjectURL(objectUrl)
                }


            })
        }
    }
})